package com.ip;

import com.graphics.*;
import com.os.OSAttribute;
import com.os.ObjectSprite;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import mapgenerator.Main;

/**
 *
 * @author Sajarwo Anggai
 */
public class PatternContainer {

    private ArrayList listImagePatternMapping;
    private ArrayList <ImageIdentity>listImagePattern;
    private HashMap<String, ImageIdentity> patternMappingContainer;
    private Main comp;

    public PatternContainer(Main comp) {
        this.comp = comp;
        listImagePatternMapping = new ArrayList();
        listImagePattern = new ArrayList();
        patternMappingContainer = new HashMap();
    }

    public synchronized boolean addImage(String imageName, int rows, int cols, boolean asSprite) {
        if (listImagePatternMapping.contains(imageName)) {
            return false;
        } else {
            ImageIdentity imageIdentity = new ImageIdentity(comp, imageName, rows, cols);
            imageIdentity.setSprite(asSprite);
            listImagePatternMapping.add(imageName);
            listImagePattern.add(imageIdentity);
            patternMappingContainer.put(imageName, imageIdentity);
            return true;
        }
    }

    public void clearAll() {
        listImagePatternMapping.clear();
        listImagePattern.clear();
        patternMappingContainer.clear();
    }

    public ArrayList getListImagePatternMapping() {
        return listImagePatternMapping;
    }

    public ArrayList<ImageIdentity> getListImagePattern() {
        return listImagePattern;
    }

    public ImageIdentity getPattern(String imageName) {
        return patternMappingContainer.get(imageName);
    }

    public void setListImage(int index, String imageName) {
        System.out.println("Index = " + index + " location=" + imageName);
        try {
            String tmploc = (String) listImagePatternMapping.get(index);
            listImagePatternMapping.set(index, imageName);
            ImageIdentity imageIdentity = patternMappingContainer.get(tmploc);
            imageIdentity.setName(imageName);
            patternMappingContainer.remove(tmploc);
            patternMappingContainer.put(imageName, imageIdentity);
        } catch (Exception e) {
        }
    }

    public synchronized void remove(String imageName) {
        try {
            int index = listImagePatternMapping.indexOf(imageName);
            listImagePatternMapping.remove(imageName);
            eraseSprite((ImageIdentity) listImagePattern.get(index));
            listImagePattern.remove(index);
            patternMappingContainer.remove(imageName);
        } catch (Exception e) {
        }
    }

    private void eraseSprite(ImageIdentity errase) {
        if (errase.isSprite()) {
            ArrayList al = comp.osContainer.getSprites();
            for (int i = 0; i < al.size(); i++) {
                ObjectSprite os = (ObjectSprite) al.get(i);
                Iterator itr = os.getAllAttribute().keySet().iterator();
                ArrayList<String> mapingKey = new ArrayList<String>();
                while (itr.hasNext()) {
                    String key = (String) itr.next();
                    OSAttribute oa = os.getAttribute(key);
                    if (oa.getAttrib() == errase) {
                        os.getAllAttribute().remove(key);
                    }
                }
            }
            comp.osControlPanel.showOnWorkspace(comp.WorkspaceBottomSprite, comp.osControlPanel.getCurrenObjectSpriteOnWorkspace());
        }
    }
}
